package com.be.beadmin.project.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.project.dto.input.InfoAndPage;
import com.be.beadmin.project.dto.input.ProductInfoDto;
import com.be.beadmin.project.dto.output.ProductInfoEx;
import com.be.beadmin.project.service.ProductInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author zjm
* @date 2023-02-13
**/
@Api(tags = {"项目管理-柜体信息模块"})
@RestController
public class ProductInfoController {

    @Autowired
    private ProductInfoService productInfoService;

    @ApiOperation("分页获取柜体信息")
    @PostMapping(value = "/project/getProductInfoByPage")
    public ResultModel getProductInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(productInfoService.getProductInfoByPage(pageEntity)) ;
    }
    @ApiOperation("根据项目ID分页获取柜体信息")
    @PostMapping(value = "/project/getProductInfoByPageAndInfoId")
    public ResultModel getProductInfoByPageAndInfoId(@RequestBody InfoAndPage infoAndPage)  {
        return ResultModel.success(productInfoService.getProductInfoByPageAndInfoId(infoAndPage)) ;
    }

    /**
     * 删除一条柜体信息
     * @param productInfoDto
     * @return
     */
    @ApiOperation("删除一条柜体信息")
    @PostMapping("/project/delProductInfo")
    public ResultModel delProductInfo(@RequestBody ProductInfoDto productInfoDto){
        int result = productInfoService.delProductInfo(productInfoDto.getId());
//        if (result==-10)  return ResultModel.error(result,"该柜体已发布，无法删除");
        if (result==-11)  return ResultModel.error(result,"该柜体已下发，无法删除");
        return ResultModel.success(ResultModel.DELETE);
    }





//    @ApiOperation("发布一条柜体信息")
//    @PostMapping("/project/releaseProductInfo")
//    public ResultModel releaseProductInfo(@RequestParam String productInfoId){
//        int result = productInfoService.releaseProductInfo(productInfoId);
//        if (result == -10)  return ResultModel.error(result,"该柜体已经发布");
//        if (result == -11)  return ResultModel.error(result,"请先发布项目");
//        if (result == -12)  return ResultModel.error(result,"请先选择一个柜体");
//
//        return ResultModel.success(result);
//    }

//    @ApiOperation("批量发布柜体信息")
//    @PostMapping("/project/releaseProductInfos")
//    public ResultModel releaseProductInfos(@RequestBody List<String> productInfoIds){
//        int result = productInfoService.releaseProductInfos(productInfoIds);
//
//        if (result == -10)  return ResultModel.error(result,"所有被选中的柜体均已发布");
//        if (result == -11)  return ResultModel.error(result,"请先发布项目");
//        if (result == -12)  return ResultModel.error(result,"请先选择柜体");
//
//        return ResultModel.success(result);
//    }

    @ApiOperation("导出柜体信息")
    @PostMapping("/project/exportProductInfo")
    public void exportProductInfo(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<ProductInfoEx> productInfoPage = productInfoService.getProductInfoByPage(pageEntity);
        List<ProductInfoEx> productInfoExes = productInfoPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("ProductInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ProductInfoEx.class).sheet("sheet1").doWrite(productInfoExes);
    }

    @ApiOperation("根据项目名称获取柜体信息")
    @PostMapping("/project/getProductsByProjectName")
    public ResultModel getProductsByProjectName(@RequestParam String projectName){
        return ResultModel.success("success" , productInfoService.getProductsByProjectName(projectName));
    }
}